package com.ruoyi.mobile.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mobile.domain.*;
import com.ruoyi.mobile.mapper.LessonPointPrescriptionItemLessonMapper;
import com.ruoyi.mobile.mapper.LessonPointPrescriptionItemMapper;
import com.ruoyi.mobile.model.bo.LessonPointPrescriptionBo;
import com.ruoyi.mobile.model.request.LessonPointPrescriptionRequest;
import com.ruoyi.mobile.model.vo.LessonPointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.LessonPointPrescriptionMapper;
import com.ruoyi.mobile.service.ILessonPointPrescriptionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 指定分数运动处方配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-10
 */
@Service
public class LessonPointPrescriptionServiceImpl implements ILessonPointPrescriptionService 
{
    @Autowired
    private LessonPointPrescriptionMapper lessonPointPrescriptionMapper;

    @Autowired
    private LessonPointPrescriptionItemMapper lessonPointPrescriptionItemMapper;

    @Autowired
    private LessonPointPrescriptionItemLessonMapper lessonPointPrescriptionItemLessonMapper;

    /**
     * 查询指定分数运动处方配置
     * 
     * @param id 指定分数运动处方配置主键
     * @return 指定分数运动处方配置
     */
    @Override
    public LessonPointPrescription selectLessonPointPrescriptionById(Integer id)
    {
        LessonPointPrescription lessonPointPrescription = lessonPointPrescriptionMapper.selectLessonPointPrescriptionById(id);
        List<LessonPointPrescription.LessonPartBean> lessonPartBeans = new ArrayList<>();
        for (LessonPointPrescriptionItem lessonPointPrescriptionItem : lessonPointPrescription.getLessonPointPrescriptionItems()) {
            LessonPointPrescription.LessonPartBean lessonPartBean = new LessonPointPrescription.LessonPartBean();
            lessonPartBean.setId(lessonPointPrescriptionItem.getId());
            lessonPartBean.setTitle(lessonPointPrescriptionItem.getTitle());
            lessonPartBean.setCover(lessonPointPrescriptionItem.getCover());
            lessonPartBean.setItemTime(lessonPointPrescriptionItem.getItemTime());

            //构建dayListBean
            List<LessonPointPrescription.LessonPartBean.DayListBean> dayListBeans = new ArrayList<>();
            List<LessonPointPrescriptionItemLesson> lessonPointPrescriptionItemLessons = lessonPointPrescriptionItemLessonMapper.getListByItemId(lessonPointPrescriptionItem.getId());
            for (LessonPointPrescriptionItemLesson lessonPointPrescriptionItemLesson : lessonPointPrescriptionItemLessons) {
                LessonPointPrescription.LessonPartBean.DayListBean dayListBean = new LessonPointPrescription.LessonPartBean.DayListBean();
                dayListBean.setId(lessonPointPrescriptionItemLesson.getId());
                dayListBean.setTitle(lessonPointPrescriptionItemLesson.getTitle());

                List<LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean> lessonListBeans = new ArrayList<>();
                String[] lessonIdStrList = lessonPointPrescriptionItemLesson.getLessonId().split(",");
                for (String lessonId : lessonIdStrList) {
                    if("".equals(lessonId)) {
                        continue;
                    }
                    LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean lessonListBean = new LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean();
                    lessonListBean.setLessonId(lessonId);
                    lessonListBeans.add(lessonListBean);
                }
                dayListBean.setLessonList(lessonListBeans);
                dayListBeans.add(dayListBean);
            }

            lessonPartBean.setDayList(dayListBeans);

            lessonPartBeans.add(lessonPartBean);
        }
        lessonPointPrescription.setLessonPart(lessonPartBeans);
        return lessonPointPrescription;
    }

    /**
     * 查询指定分数运动处方配置列表
     * 
     * @param lessonPointPrescription 指定分数运动处方配置
     * @return 指定分数运动处方配置
     */
    @Override
    public List<LessonPointPrescriptionBo> selectLessonPointPrescriptionList(LessonPointPrescription lessonPointPrescription)
    {
        List<LessonPointPrescriptionBo> list = lessonPointPrescriptionMapper.selectLessonPointPrescriptionList(lessonPointPrescription);
        return list;
    }

    /**
     * 新增指定分数运动处方配置
     * 
     * @param LessonPointPrescriptionRequest 指定分数运动处方配置
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLessonPointPrescription(LessonPointPrescriptionRequest lessonPointPrescriptionRequest)
    {
        LessonPointPrescription lessonPointPrescription = new LessonPointPrescription();
        lessonPointPrescription.setPoint(lessonPointPrescriptionRequest.getPoint());
        lessonPointPrescription.setPrice(lessonPointPrescriptionRequest.getPrice());
        lessonPointPrescription.setDesc(lessonPointPrescriptionRequest.getDesc());
        lessonPointPrescription.setProhibition(lessonPointPrescriptionRequest.getProhibition());
        lessonPointPrescription.setAttention(lessonPointPrescriptionRequest.getAttention());
        lessonPointPrescription.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        lessonPointPrescription.setCreateTime(DateUtils.getNowDate());
        lessonPointPrescriptionMapper.insertLessonPointPrescription(lessonPointPrescription);


        if (!lessonPointPrescriptionRequest.getLessonPart().isEmpty()) {
            for (LessonPointPrescriptionRequest.LessonPartBean lessonPartBean : lessonPointPrescriptionRequest.getLessonPart()) {
                LessonPointPrescriptionItem lessonPointPrescriptionItem = new LessonPointPrescriptionItem();
                if (StrUtil.isBlank(lessonPartBean.getCover())) {
                    throw new ServiceException("请选择封面");
                }
                if (StrUtil.isBlank(lessonPartBean.getTitle())) {
                    throw new ServiceException("请选择标题");
                }
                if (lessonPartBean.getItemTime() == null || lessonPartBean.getItemTime() < 0) {
                    throw new ServiceException("请选择课程时长");
                }
                lessonPointPrescriptionItem.setCover(lessonPartBean.getCover());
                lessonPointPrescriptionItem.setTitle(lessonPartBean.getTitle());
                lessonPointPrescriptionItem.setItemTime(lessonPartBean.getItemTime());
                lessonPointPrescriptionItem.setPrescriptionId(lessonPointPrescription.getId());
                lessonPointPrescriptionItemMapper.insertLessonPointPrescriptionItem(lessonPointPrescriptionItem);

                for (LessonPointPrescriptionRequest.LessonPartBean.DayListBean dayListBean : lessonPartBean.getDayList()) {
                    LessonPointPrescriptionItemLesson lessonPointPrescriptionItemLesson = new LessonPointPrescriptionItemLesson();
                    lessonPointPrescriptionItemLesson.setItemId(lessonPointPrescriptionItem.getId());
                    lessonPointPrescriptionItemLesson.setPrescriptionId(lessonPointPrescription.getId());
                    lessonPointPrescriptionItemLesson.setTitle(dayListBean.getTitle());

                    String lessonId = "";
                    if (!dayListBean.getLessonList().isEmpty()) {
                        for (LessonPointPrescriptionRequest.LessonPartBean.DayListBean.LessonListBean lessonListBean : dayListBean.getLessonList()) {
                            if (lessonListBean.getLessonId() == null) {
                                continue;
                            }
                            lessonId = "".equals(lessonId) ? lessonListBean.getLessonId().toString() : lessonId + "," + lessonListBean.getLessonId().toString();
                        }
                    }
                    lessonPointPrescriptionItemLesson.setLessonId(lessonId);
                    lessonPointPrescriptionItemLessonMapper.insertLessonPointPrescriptionItemLesson(lessonPointPrescriptionItemLesson);

                }

            }
        }
        return 1;
    }

    /**
     * 修改指定分数运动处方配置
     * 
     * @param LessonPointPrescriptionRequest 指定分数运动处方配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateLessonPointPrescription(LessonPointPrescriptionRequest lessonPointPrescriptionRequest)
    {
        LessonPointPrescription lessonPointPrescription = new LessonPointPrescription();
        lessonPointPrescription.setId(lessonPointPrescriptionRequest.getId());
        lessonPointPrescription.setPoint(lessonPointPrescriptionRequest.getPoint());
        lessonPointPrescription.setPrice(lessonPointPrescriptionRequest.getPrice());
        lessonPointPrescription.setDesc(lessonPointPrescriptionRequest.getDesc());
        lessonPointPrescription.setProhibition(lessonPointPrescriptionRequest.getProhibition());
        lessonPointPrescription.setAttention(lessonPointPrescriptionRequest.getAttention());
        lessonPointPrescription.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        lessonPointPrescription.setCreateTime(DateUtils.getNowDate());
        lessonPointPrescriptionMapper.updateLessonPointPrescription(lessonPointPrescription);

        //检查这个lessonPrescription底下有多少个item，有些item可能要被删掉
        List<Integer> itemIdList = lessonPointPrescriptionItemMapper.getIdListByPrescriptionId(lessonPointPrescriptionRequest.getId());

        if (!lessonPointPrescriptionRequest.getLessonPart().isEmpty()) {
            for (LessonPointPrescriptionRequest.LessonPartBean lessonPartBean : lessonPointPrescriptionRequest.getLessonPart()) {
                LessonPointPrescriptionItem lessonPointPrescriptionItem = new LessonPointPrescriptionItem();
                lessonPointPrescriptionItem.setCover(lessonPartBean.getCover());
                lessonPointPrescriptionItem.setTitle(lessonPartBean.getTitle());
                lessonPointPrescriptionItem.setItemTime(lessonPartBean.getItemTime());
                lessonPointPrescriptionItem.setPrescriptionId(lessonPointPrescription.getId());
                if (lessonPartBean.getId() != null) {
                    //编辑lessonPrescriptionItem
                    if (itemIdList.contains(lessonPartBean.getId())) {
                        itemIdList.remove(lessonPartBean.getId());
                    }
                    lessonPointPrescriptionItem.setId(lessonPartBean.getId());
                    lessonPointPrescriptionItemMapper.updateLessonPointPrescriptionItem(lessonPointPrescriptionItem);
                } else {
                    //新增lessonPrescriptionItem
                    lessonPointPrescriptionItemMapper.insertLessonPointPrescriptionItem(lessonPointPrescriptionItem);
                }

                //检查这个lessonPrescriptionItem底下有多少个lesson，有些lesson可能要被删掉
                List<Integer> itemLessonIdList = lessonPointPrescriptionItemLessonMapper.getIdListByItemId(lessonPointPrescriptionItem.getId());

                for (LessonPointPrescriptionRequest.LessonPartBean.DayListBean dayListBean : lessonPartBean.getDayList()) {
                    LessonPointPrescriptionItemLesson lessonPointPrescriptionItemLesson = new LessonPointPrescriptionItemLesson();
                    lessonPointPrescriptionItemLesson.setItemId(lessonPointPrescriptionItem.getId());
                    lessonPointPrescriptionItemLesson.setPrescriptionId(lessonPointPrescription.getId());
                    lessonPointPrescriptionItemLesson.setTitle(dayListBean.getTitle());

                    String lessonId = "";
                    if (!dayListBean.getLessonList().isEmpty()) {
                        for (LessonPointPrescriptionRequest.LessonPartBean.DayListBean.LessonListBean lessonListBean : dayListBean.getLessonList()) {
                            if (lessonListBean.getLessonId() == null) {
                                continue;
                            }
                            lessonId = "".equals(lessonId) ? lessonListBean.getLessonId().toString() : lessonId + "," + lessonListBean.getLessonId().toString();
                        }
                    }
                    lessonPointPrescriptionItemLesson.setLessonId(lessonId);
                    //检查是新增还是编辑
                    if (dayListBean.getId() != null) {
                        //编辑lessonPrescriptionItemLesson
                        if (itemLessonIdList.contains(dayListBean.getId())) {
                            itemLessonIdList.remove(dayListBean.getId());
                        }
                        lessonPointPrescriptionItemLesson.setId(dayListBean.getId());
                        lessonPointPrescriptionItemLessonMapper.updateLessonPointPrescriptionItemLesson(lessonPointPrescriptionItemLesson);
                    } else {
                        //增加lessonPrescriptionItemLesson
                        lessonPointPrescriptionItemLessonMapper.insertLessonPointPrescriptionItemLesson(lessonPointPrescriptionItemLesson);
                    }

                }

                if (!itemLessonIdList.isEmpty()) {
                    lessonPointPrescriptionItemLessonMapper.deleteByIds(itemLessonIdList);
                }
            }
        }

        if (!itemIdList.isEmpty()) {
            lessonPointPrescriptionItemMapper.deleteByIds(itemIdList);
            lessonPointPrescriptionItemLessonMapper.deleteByItemIds(itemIdList);
        }


        return 1;
    }

    /**
     * 批量删除指定分数运动处方配置
     * 
     * @param ids 需要删除的指定分数运动处方配置主键
     * @return 结果
     */
    @Override
    public int deleteLessonPointPrescriptionByIds(Integer[] ids)
    {
        lessonPointPrescriptionMapper.deleteLessonPointPrescriptionByIds(ids);
        lessonPointPrescriptionItemMapper.deleteLessonPrescriptionByPrescriptionIds(ids);
        return lessonPointPrescriptionItemLessonMapper.deleteLessonPointPrescriptionByIds(ids);
    }


    /**
     * 查询指定分数运动处方配置
     *
     * @param point 分数
     * @return 指定分数运动处方配置
     */
    @Override
    public LessonPointPrescription selectLessonPointPrescriptionByPoint(BigDecimal point)
    {
        LessonPointPrescription lessonPointPrescription = lessonPointPrescriptionMapper.selectLessonPointPrescriptionByPoint(point);
        List<LessonPointPrescription.LessonPartBean> lessonPartBeans = new ArrayList<>();
        for (LessonPointPrescriptionItem lessonPointPrescriptionItem : lessonPointPrescription.getLessonPointPrescriptionItems()) {
            LessonPointPrescription.LessonPartBean lessonPartBean = new LessonPointPrescription.LessonPartBean();
            lessonPartBean.setId(lessonPointPrescriptionItem.getId());
            lessonPartBean.setTitle(lessonPointPrescriptionItem.getTitle());
            lessonPartBean.setCover(lessonPointPrescriptionItem.getCover());
            lessonPartBean.setItemTime(lessonPointPrescriptionItem.getItemTime());

            //构建dayListBean
            List<LessonPointPrescription.LessonPartBean.DayListBean> dayListBeans = new ArrayList<>();
            List<LessonPointPrescriptionItemLesson> lessonPointPrescriptionItemLessons = lessonPointPrescriptionItemLessonMapper.getListByItemId(lessonPointPrescriptionItem.getId());
            for (LessonPointPrescriptionItemLesson lessonPointPrescriptionItemLesson : lessonPointPrescriptionItemLessons) {
                LessonPointPrescription.LessonPartBean.DayListBean dayListBean = new LessonPointPrescription.LessonPartBean.DayListBean();
                dayListBean.setId(lessonPointPrescriptionItemLesson.getId());
                dayListBean.setTitle(lessonPointPrescriptionItemLesson.getTitle());

                List<LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean> lessonListBeans = new ArrayList<>();
                String[] lessonIdStrList = lessonPointPrescriptionItemLesson.getLessonId().split(",");
                for (String lessonId : lessonIdStrList) {
                    if("".equals(lessonId)) {
                        continue;
                    }
                    LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean lessonListBean = new LessonPointPrescription.LessonPartBean.DayListBean.LessonListBean();
                    lessonListBean.setLessonId(lessonId);
                    lessonListBeans.add(lessonListBean);
                }
                dayListBean.setLessonList(lessonListBeans);
                dayListBeans.add(dayListBean);
            }

            lessonPartBean.setDayList(dayListBeans);

            lessonPartBeans.add(lessonPartBean);
        }
        lessonPointPrescription.setLessonPart(lessonPartBeans);
        return lessonPointPrescription;
    }


    /**
     * 获取所有指定分数的运动处方的分数
     * @return
     */
    @Override
    public List<LessonPointVo> selectAllLessonPoint() {
        return lessonPointPrescriptionMapper.selectAllLessonPoint();
    }
}
